package com.bac.rentmap.web.servlet;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

public class RequestParameterViewResolver extends InternalResourceViewResolver {
    private String parameterName;
    private String parameterValue;

    public View resolveViewName(String viewName, Locale locale) throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
        String parameter = request.getParameter(parameterName);
        if(parameter != null && parameter.contains(parameterValue)){
            return super.resolveViewName(viewName, locale);
        }
        return null;
    }

    @Required
    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    @Required
    public void setParameterValue(String parameterValue) {
        this.parameterValue = parameterValue;
    }
}
